// pages/appoinmentDetails/appoinmentDetails.js
var request = require("../../utils/request.js")
var amapFile = require("../../libs/amap-wx.130.js")
var config = require("../../libs/config.js")
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nowclientX: '',
    l: "",
    info: "",
    mak: {},
    doctorlist: [],
    width1: 450,
    width: 450, //宽度根据医生人数计算，112*医生人数
    latitude: "",
    longitude: "",
    starNum: 0,
    starInfo: [],

  },
  //立即预约跳转
  appoinmentBtn: function (e) {
    wx.navigateTo({
      url: '../ReservationForm/ReservationForm?cid=' + this.data.info.id + "&name=" + this.data.info.name+'&dateType=yuyue',
    })
  },
  //医生详情
  doctorDetails: function (e) {
    //console.log(e);
    wx.navigateTo({
      url: '../doctorDetails/doctorDetails?pid=' + e.currentTarget.dataset.id+'&infoName='+this.data.info.name+'&cid='+this.data.info.id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showShareMenu({ menus: ['shareAppMessage', 'shareTimeline']})
    this.toastDialog = this.selectComponent("#toastDialog"); //此处id和页面的 wxml中的id一致
    this.appoinmentDetails(options.cid)
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage (res) {
    return {
      title: '迪佳极视',
      path: 'pages/index/index',
      // imageUrl:'这个是显示的图片，不写就默认当前页面的截图',
      success: function (shareTickets) {
        console.info(shareTickets + '成功');
        // 转发成功
      },
      fail: function (res) {
        console.log(res + '失败');
        // 转发失败
      },
      complete:function(res){
        // 不管成功失败都会执行
      }
    }
  },
  //机构详情
  appoinmentDetails: function (e) {
    var that = this;
    that.toastDialog.showDialog({
      code: 'loading',
      message: '加载中',
      timeout: false
    }); //loading 加载框
    request.postData('xcx/clinicInfo', {
      "cid": e
    }, function (res) {
      console.log(res);
      that.toastDialog.hideDialog(); //关闭loading提示框
      if (res.code == 0) {
        //医生、开设项目 滑动宽度设置
        that.setData({
          info: res.info,
          doctorlist: res.doctorList,
          width: 112 * (res.doctorList.length),
          width1: 112 * (res.info.projectList.length)
        })
        //医院地址 高德地图调用
        if (res.info.lat != undefined && res.info.lon != undefined) {
          var key = config.config.key;
          var myAmapFun = new amapFile.AMapWX({
            key: key
          });
          myAmapFun.getRegeo({
            location: res.info.lat + "," + res.info.lon,
            iconPath: "../../images/icon/icon_pois.png",
            success: function (data) {
              console.log(data)
              that.setData({
                mak: data,
                longitude: data[0].longitude,
                latitude: data[0].latitude
              })
            }, fail: function (info) {
              //失败回调
              console.log(info)
            }
          })
        }
        //医院评分
        console.log(typeof that.data.info.evaluate)
        let isHalf = false; //用来判断最后一颗星是否是满星
        let starInfo = []; //存放星星，用来循环星星
        let starNum = 0;  //向上取整，用来判断有几颗星星
        if (Math.floor(that.data.info.evaluate) === that.data.info.evaluate) {
          isHalf = false
          starNum = that.data.info.evaluate;
        } else {
          isHalf = true
          starNum = Math.ceil(that.data.info.evaluate); //向上取整，用来判断有几颗星星
        }
        for (var i = 0; i < starNum; i++) {
          let obj = {}
          if (isHalf && i == starNum - 1) {
            obj = { star: "../../images/icon/star_half.png" }
          } else {
            obj = { star: "../../images/icon/star.png" }
          }
          starInfo.push(obj)
        }
        that.setData({
          starInfo: starInfo
        })
        console.log(Math.floor(that.data.info.evaluate) === that.data.info.evaluate)
        console.log(isHalf)
        console.log(that.data.starInfo)
      } else {
        that.toastDialog.showDialog({
          code: res.code,
          message: res.message,
          timeout: true
        });
      }
    }, function (res) {

      that.toastDialog.showDialog({
        code: res.code,
        message: res.message,
        timeout: true
      });
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  gotoProjectDetaile: function (e) {
    console.log(e)
    wx.navigateTo({
      url: '../project/project?pid=' + e.currentTarget.dataset.pid
    })
  }
})